package config

import (
	"common/helper"
	"database/sql"
	"math/rand"
	"time"
)

//业务员列表

const Def_Channel = "888888" //默认 官方渠道

type CfgBussinessInfo struct {
	BusinessId string `json:"id" db:"id" cbor:"id"`
	OperatorId string `json:"operator_id" db:"operator_id" cbor:"operator_id"`
}

var G_cfgBussinessInfo []CfgBussinessInfo

type CfgChannelInfo struct {
	OperatorId     string `json:"operator_id" db:"operator_id" cbor:"operator_id"`
	GameType       string `json:"game" db:"game" cbor:"game"`
	IntoFakeRate   int    `json:"into_fake_rate" db:"into_fake_rate" cbor:"into_fake_rate"`
	InitRtp        int    `json:"init_rtp" db:"init_rtp" cbor:"init_rtp"`
	AdjustRtp      int    `json:"adjust_rtp" db:"adjust_rtp" cbor:"adjust_rtp"`
	AdjustMoneyMul int    `json:"adjust_money_mul" db:"adjust_money_mul" cbor:"adjust_money_mul"`
}

var G_cfgChannelInfo []CfgChannelInfo

func BussinessInfoSetRow(rows *sql.Rows) {
	var tVal CfgBussinessInfo
	var err error
	err = rows.Scan(&tVal.BusinessId, &tVal.OperatorId)

	if err != nil {
		helper.InfoLog("BussinessInfoSetRow scan err")
		return
	}
	G_cfgBussinessInfo = append(G_cfgBussinessInfo, tVal)
}

func BussinessInfoRemove() {
	G_cfgBussinessInfo = []CfgBussinessInfo{}
}

func FindOperatorIdByBussinessId(bussinessId string) (string, bool) {
	operatorId := Def_Channel
	isFind := false
	for _, val := range G_cfgBussinessInfo {
		if val.BusinessId != bussinessId {
			continue
		}
		operatorId = val.OperatorId
		isFind = true
		break
	}

	return operatorId, isFind
}

func IsExistOperatorId(operatorId string) bool {
	if "" == operatorId {
		return false
	}

	for _, val := range G_cfgChannelInfo {
		if val.OperatorId != operatorId {
			continue
		}
		return true
	}

	return false
}

func ChannelInfoSetRow(rows *sql.Rows) {
	var Info CfgChannelInfo
	var err error
	err = rows.Scan(&Info.OperatorId, &Info.GameType, &Info.IntoFakeRate, &Info.InitRtp, &Info.AdjustRtp, &Info.AdjustMoneyMul)

	if err != nil {
		helper.InfoLog("ChannelInfoSet scan err")
		return
	}

	G_cfgChannelInfo = append(G_cfgChannelInfo, Info)
}

func ChannelInfoRemove() {
	G_cfgChannelInfo = []CfgChannelInfo{}
}
func GetOperatorInitRtp(OperatorId string, gameType string) int {

	for _, value := range G_cfgChannelInfo {
		if value.OperatorId == OperatorId && value.GameType == gameType {
			return value.InitRtp
		}
	}

	return 0
}

func GetOperatorAdjustInfo(OperatorId string, gameType string) (int, int) {

	var AdjustRtpOffical int = 0
	var AdjustMoneyMulOffical int = 0

	for _, value := range G_cfgChannelInfo {
		if value.OperatorId == Def_Channel {
			AdjustRtpOffical = value.AdjustRtp
			AdjustMoneyMulOffical = value.AdjustMoneyMul
		}
		if value.OperatorId == OperatorId &&
			value.GameType == gameType &&
			value.AdjustRtp != 0 &&
			value.AdjustMoneyMul != 0 {
			return value.AdjustRtp, value.AdjustMoneyMul
		}
	}

	//本渠道没有找到 找官方888888
	return AdjustRtpOffical, AdjustMoneyMulOffical
}
func CanEntryFakceGameByChannelId(OperatorId string, gameType string) bool {
	var EntryFakePGRate int = 100

	for _, value := range G_cfgChannelInfo {
		if value.OperatorId == OperatorId && value.GameType == gameType {
			EntryFakePGRate = value.IntoFakeRate
			break
		}
	}

	rand.New(rand.NewSource(time.Now().UnixNano()))
	randValue := rand.Intn(100) //[0,99]

	if EntryFakePGRate > randValue {
		return true
	} else {
		return false
	}
}
